package com.malou.client.seguridad.usuario;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.malou.client.MalouConstants;
import com.malou.client.util.Utilities;

public class GrupoDialog extends Dialog {

	private HiddenField<String> id;
	public HiddenField<String> parent_id;
	protected TextField<String> usuario;
	private Button cancelar;
	private Button guardar;
	public Status status;
	public UsuarioPanel usuarioPanel;
	private FormBinding formBindings;
	private FormPanel panel;

	private MalouConstants constants = GWT.create(MalouConstants.class);

	public GrupoDialog() {

		super();
		setModal(true);
		setSize(280, 110);
		setButtonAlign(HorizontalAlignment.LEFT);
		setButtons("");
		setResizable(false);
		
		panel = new FormPanel();
		panel.setBorders(false);
		panel.setBodyBorder(false);
		panel.setPadding(5);
		panel.setHeaderVisible(false);
		panel.setLabelWidth(50);

		formBindings = new FormBinding(panel, false);
		addFields();

		
		setFocusWidget(usuario);
		Component com[]={usuario,guardar};
		Utilities.Enter(com);
	}

	protected void createButtons() {
		super.createButtons();

		if (constants == null) {
			constants = GWT.create(MalouConstants.class);
		}

		status = new Status();
		status.setBusy(constants.guardando());
		status.hide();
		status.setAutoWidth(true);
		getButtonBar().add(status);

		getButtonBar().add(new FillToolItem());

		cancelar = new Button(constants.cancelar(),
				IconHelper.createPath("images/icons/cancel.png"));
		cancelar.setSize(70, 25);

		cancelar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				GrupoDialog.this.hide();
				Usuarios.store.rejectChanges();
			}

		});

		guardar = new Button(constants.guardar(),
				IconHelper.createPath("images/icons/save_all.png"));
		guardar.setSize(70, 25);

		guardar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			public void componentSelected(ButtonEvent ce) {
				if (validate()) {
					status.show();
					getButtonBar().disable();

					Usuario user = new Usuario();
					if (id.getValue() != null)
						user.setId(Integer.valueOf(id.getValue()));
					if (parent_id.getValue() != null)
						user.setParent_id(Integer.valueOf(parent_id.getValue()));
					user.setUsuario(usuario.getValue());

					Usuarios.grupoDialog = GrupoDialog.this;
					Usuarios.saveGroup(user);
				}
			}
		});

		addButton(guardar);
		addButton(cancelar);

	}

	private void addFields() {

		id = new HiddenField<String>();
		id.setVisible(false);
		panel.add(id);

		parent_id = new HiddenField<String>();
		parent_id.setVisible(false);
		panel.add(parent_id);

		usuario = new TextField<String>();
		usuario.setMinLength(3);
		usuario.setFieldLabel(constants.usuarioGrupo());
		usuario.setAllowBlank(false);
		panel.add(usuario, new FormData("95%"));
		
		add(panel);

	}

	public void beforeAdd() {
		setIconStyle("icon-group");
		setHeading(constants.usuarioNuevoGrupo());
	}

	public void beforeEdit() {
		setIconStyle("icon-update");
		setHeading(constants.usuarioModificarGrupo());
		status.hide();
		getButtonBar().enable();
	}

	public void reset() {
		id.clear();
		usuario.clear();
		parent_id.clear();
		status.hide();
		getButtonBar().enable();
	}

	public void setStore(TreeStore<Usuario> store) {

		formBindings.addFieldBinding(new FieldBinding(id, "id"));
		formBindings.addFieldBinding(new FieldBinding(parent_id, "parent_id"));
		formBindings.addFieldBinding(new FieldBinding(usuario, "usuario"));

		formBindings.setStore(store);
	}

	protected boolean validate() {
		boolean gr = usuario.isValid();
		return gr;
	}

	public void bind(Usuario selectedModel) {
		formBindings.bind(selectedModel);
	}

	public void unbind() {
		formBindings.unbind();
	}
}
